package ssw.gui.japplet;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.TreeSet;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import ssw.game.Game;
import ssw.game.GameState;
import ssw.game.GameType;

public class GameJPanel extends JPanel implements ActionListener {
	private static final String LBL_WORD = "Word: ";
	
	private final AppCore bridge;

	private final JTextArea taWords = new JTextArea(4, 20);

	private final JLabel lblWord = new JLabel(LBL_WORD);
	private final JButton[] btnTiles = new JButton[7];

	private final JButton btnAdd = new JButton("Add");

	private final JButton btnClear = new JButton("Clear");

	private final JButton btnDone = new JButton("Done");

	private final TreeSet<String> guesses = new TreeSet<String>();
	
	public GameJPanel(final AppCore b) {
		super(new GridBagLayout());
		bridge = b;
		initComponents();
	}

	private void resetTiles()  {
		for (JButton jb : btnTiles)  {
			jb.setEnabled(true);
		}
		lblWord.setText(LBL_WORD);
	}
	
	@Override
	public void actionPerformed(final ActionEvent e) {
		System.out.println(e);
		if (e.getSource() == btnClear)  {
			resetTiles();
		}
		else if (e.getSource() instanceof TileButton) {
			final TileButton tb = (TileButton) e.getSource();
			System.out.println("Tile: " + tb.getText());
			lblWord.setText(lblWord.getText() + tb.getText());
			tb.setEnabled(false);
		}
		else if (e.getSource() == btnAdd)  {
			String word = lblWord.getText().substring(LBL_WORD.length());
			System.out.println(word);
			
			if (word.length() > 0)  {
				if (guesses.contains(word))  {
					guesses.remove(word);
				}
				else  {
					guesses.add(word);
				}
				
				taWords.setText(guesses.toString());
				resetTiles();
			}
		}
		else if (e.getSource() == btnDone)  {
			bridge.submitGuesses(guesses);
		}
	}

	private void initComponents() {
		GridBagConstraints gbc = null;
		int y = 0;

		gbc = new GridBagConstraints();
		gbc.gridy = y;
		gbc.gridx = 0;
		gbc.gridwidth = 7;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		final JScrollPane spWords = new JScrollPane(taWords,
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		add(spWords, gbc);

		y++;

		gbc = new GridBagConstraints();
		gbc.gridy = y;
		gbc.gridx = 0;
		gbc.gridwidth = 7;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		add(lblWord, gbc);

		y++;

		for (int i1 = 0; i1 < 7; i1++) {
			final TileButton tb = new TileButton("-", this);
			btnTiles[i1] = tb;
			gbc = new GridBagConstraints();
			gbc.gridy = y;
			gbc.gridx = i1;
			add(tb, gbc);
		}

		y++;

		gbc = new GridBagConstraints();
		gbc.gridy = y;
		gbc.gridx = 0;
		gbc.gridwidth = 5;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		add(btnAdd, gbc);
		btnAdd.addActionListener(this);

		gbc = new GridBagConstraints();
		gbc.gridy = y;
		gbc.gridx = 5;
		gbc.gridwidth = 2;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		add(btnClear, gbc);
		btnClear.addActionListener(this);
		
		y++;

		gbc = new GridBagConstraints();
		gbc.gridy = y;
		gbc.gridx = 0;
		gbc.gridwidth = 7;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		add(btnDone, gbc);
		btnDone.addActionListener(this);
	}

	public void setup(final GameState gameState) {
		System.out.println(gameState);
		
		int tileX = 0;
		for (final String letter : gameState.getTiles()) {
			btnTiles[tileX].setText(letter);
			tileX++;
		}
		
		guesses.clear();
		taWords.setText("");
	}
}

class TileButton extends JButton {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3850020900424177326L;

	TileButton(final String text, final ActionListener listener) {
		super(text);
		addActionListener(listener);
	}
}